package com.sdtele.tta.operation;

import java.util.List;

import com.google.gson.JsonObject;
import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.User;

/**
 * 操作类实现：Login(json.operation=login)
 * @author XCream
 *
 */
public class OperationLogin extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		System.out.println("OperationLogin:req="+jsonRequest);
		JsonResponse jres = new JsonResponse();
		
		String name =jsonRequest.getJsonObject().get("username").getAsString();
		System.out.println(name);
		String passwd =jsonRequest.getJsonObject().get("password").getAsString();
		
		System.out.println(passwd);
		User user = Dac.getInstance().getUser(name, passwd);
		if(user!=null)
		{			
			jres.setStatus(0);
			jres.setMessage("登录成功");
			jres.setEntity(user);
			request.getSession().setAttribute("user", user);
			//System.out.println(request.getSession().getAttribute("user"));
		}else
		{
			jres.setStatus(1);
			jres.setMessage("用户名或者密码错误");
			jres.setEntity("null");
		}
		
		return jres;
	}

}
